<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="pname">
        <el-input
          v-model="queryParams.pname"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="sku规格" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入sku规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="punit">
        <el-input
          v-model="queryParams.punit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包装量" prop="unitNum">
        <el-input
          v-model="queryParams.unitNum"
          placeholder="请输入包装量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代发价格" prop="dropShippingPrice">
        <el-input
          v-model="queryParams.dropShippingPrice"
          placeholder="请输入代发价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售价格" prop="retailPrice">
        <el-input
          v-model="queryParams.retailPrice"
          placeholder="请输入销售价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制造商" prop="manufacture">
        <el-input
          v-model="queryParams.manufacture"
          placeholder="请输入制造商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="佣金" prop="commission">
        <el-input
          v-model="queryParams.commission"
          placeholder="请输入佣金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述链接" prop="descUrl">
        <el-input
          v-model="queryParams.descUrl"
          placeholder="请输入描述链接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支持快递" prop="express">
        <el-input
          v-model="queryParams.express"
          placeholder="请输入支持快递"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pallet:pallets:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pallet:pallets:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pallet:pallets:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pallet:pallets:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="palletsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" v-if="columns[0].visible" prop="id" />
        <el-table-column label="商品名称" :show-overflow-tooltip="true" align="center" v-if="columns[1].visible" prop="pname" />
        <el-table-column label="sku规格" :show-overflow-tooltip="true" align="center" v-if="columns[2].visible" prop="specification" />
        <el-table-column label="单位" :show-overflow-tooltip="true" align="center" v-if="columns[3].visible" prop="punit" />
        <el-table-column label="包装量" :show-overflow-tooltip="true" align="center" v-if="columns[4].visible" prop="unitNum" />
        <el-table-column label="代发价格" :show-overflow-tooltip="true" align="center" v-if="columns[5].visible" prop="dropShippingPrice" />
        <el-table-column label="销售价格" :show-overflow-tooltip="true" align="center" v-if="columns[6].visible" prop="retailPrice" />
        <el-table-column label="图片" align="center" v-if="columns[7].visible" prop="imageUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
        <el-table-column label="供应商" :show-overflow-tooltip="true" align="center" v-if="columns[8].visible" prop="supplier" />
        <el-table-column label="制造商" :show-overflow-tooltip="true" align="center" v-if="columns[9].visible" prop="manufacture" />
        <el-table-column label="佣金" :show-overflow-tooltip="true" align="center" v-if="columns[10].visible" prop="commission" />
        <el-table-column label="描述链接" :show-overflow-tooltip="true" align="center" v-if="columns[11].visible" prop="descUrl" />
        <el-table-column label="支持快递" :show-overflow-tooltip="true" align="center" v-if="columns[12].visible" prop="express" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pallet:pallets:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pallet:pallets:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改货盘对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="pname">
          <el-input v-model="form.pname" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="sku规格" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入sku规格" />
        </el-form-item>
        <el-form-item label="单位" prop="punit">
          <el-input v-model="form.punit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="包装量" prop="unitNum">
          <el-input v-model="form.unitNum" placeholder="请输入包装量" />
        </el-form-item>
        <el-form-item label="代发价格" prop="dropShippingPrice">
          <el-input v-model="form.dropShippingPrice" placeholder="请输入代发价格" />
        </el-form-item>
        <el-form-item label="销售价格" prop="retailPrice">
          <el-input v-model="form.retailPrice" placeholder="请输入销售价格" />
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl">
          <image-upload v-model="form.imageUrl"/>
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="制造商" prop="manufacture">
          <el-input v-model="form.manufacture" placeholder="请输入制造商" />
        </el-form-item>
        <el-form-item label="佣金" prop="commission">
          <el-input v-model="form.commission" placeholder="请输入佣金" />
        </el-form-item>
        <el-form-item label="描述链接" prop="descUrl">
          <el-input v-model="form.descUrl" placeholder="请输入描述链接" />
        </el-form-item>
        <el-form-item label="支持快递" prop="express">
          <el-input v-model="form.express" placeholder="请输入支持快递" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPallets, getPallets, delPallets, addPallets, updatePallets } from "@/api/pallet/pallets";

export default {
  name: "Pallets",
  data() {
    return {
      //表格展示列
      columns: [
        { key: 0, label: 'id', visible: true },
          { key: 1, label: '商品名称', visible: true },
          { key: 2, label: 'sku规格', visible: true },
          { key: 3, label: '单位', visible: true },
          { key: 4, label: '包装量', visible: true },
          { key: 5, label: '代发价格', visible: true },
          { key: 6, label: '销售价格', visible: true },
          { key: 7, label: '图片', visible: true },
          { key: 8, label: '供应商', visible: true },
          { key: 9, label: '制造商', visible: true },
          { key: 10, label: '佣金', visible: true },
          { key: 11, label: '描述链接', visible: true },
          { key: 12, label: '支持快递', visible: true },
        ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货盘表格数据
      palletsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 支持快递时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pname: null,
        specification: null,
        punit: null,
        unitNum: null,
        dropShippingPrice: null,
        retailPrice: null,
        imageUrl: null,
        supplier: null,
        manufacture: null,
        commission: null,
        descUrl: null,
        express: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询货盘列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listPallets(this.queryParams).then(response => {
        this.palletsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pname: null,
        specification: null,
        punit: null,
        unitNum: null,
        dropShippingPrice: null,
        retailPrice: null,
        imageUrl: null,
        supplier: null,
        manufacture: null,
        commission: null,
        createTime: null,
        descUrl: null,
        express: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货盘";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPallets(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货盘";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePallets(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPallets(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除货盘编号为"' + ids + '"的数据项？').then(function() {
        return delPallets(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pallet/pallets/export', {
        ...this.queryParams
      }, `pallets_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
